#ifndef BALANCA_H
#define BALANCA_H

#include <QObject>
#include <QSerialPort>
#include <QString>
#include <QVariantMap>

#define DEFAULT_BAUD QSerialPort::Baud9600

#define K_MENSAGEM "mensagem"
#define K_PESO "peso"
#define K_VALOR_POR_KG "valor_kg"
#define K_VALOR_TOTAL "valor"
#define K_CODIGO_PRODUTO "codigo"

class Balanca : public QObject
{
    Q_OBJECT
public:
    explicit Balanca(QObject *parent = 0);

    bool abrir_conexao();
    void fechar_conexao();

    virtual bool checar_peso(double peso);
    virtual QVariantMap get_dados() {return QVariantMap();};
    virtual void enviar_preco(double preco) {};

    QString port();
    void setPort(QString port);

    QString baudrate();
    void setBaudrate(QString baudrate);

    int timeoutInMillis();
    void setTimeoutInMillis(int timeoutInMillis);

    virtual float readIntervalInSeconds();

signals:

public slots:

protected:
    int m_timeoutInMillis;

    int readByte(char *byte);
    int writeByte(char byte);
    int writeData(char *buf, int bufSize);

    bool waitForReadyRead(unsigned long long timeoutInMillis);
    unsigned long long currentTimeInMillis();

private:
    QSerialPort m_serialPort;
    QString m_port;
    QString m_baudrate;

    qint32 getBaudFromString(QString baud);
};

#endif // BALANCA_H
